/** Class Name: OORLeadsReassignController
 * Author: Accenture
 * Requirement/Project Name: OOR 
 * Requirement Description: Controller for Reassign Leads Button.
 */
 
public class OORLeadsReassignController {
    public String finalURL{get;set;}//String to pass URL after Reassign
    public String validUser{get;set;}//String to pass value if user is a market group user or not
    public String NoUpdate{get;set;}//String to pass URL on any validation
    Public Integer Total{get; set;}//Total no of records selected
    Public Integer NoUpdateInt{get; set;}//No of records not getting Updated with owner = User
    Public Integer Noassign{get; set;}//No of records not getting Updated
    public String assignmentStatus{get;set;}
    ApexPages.StandardSetController setCon;
    list<lead> userLeadList= new list<lead>();//Lead list to go for reassignment
    Map<Id,Lead> LeadMapList = new Map<Id,Lead>();//Map to hold Lead id against the Lead Sobject
    Map<Id,Lead> LeadMapListUser = new Map<Id,Lead>();//Map to hold Owner Id of lead agaist Lead Sobject
    Set<String> LeadIdSet = new Set<String>();//Lead Id set
    Set<String> OfficeStr = new Set<String>();//Office Code Set
    
    public OORLeadsReassignController(ApexPages.StandardSetController stdSetController) {
       setCon = stdSetController; 
       validUser ='NO';
       assignmentStatus ='';
       
       UserRole usrRole  = [select Name from UserRole  where id  =:userinfo.getUserRoleId()];
        for (String str:OORTextConstant.OOR_USER_ROLE_NAMES){
            // Check if loged in user is Markets grp - User * 
            if(str.equalsIgnoreCase(usrRole.name)){
                 validUser = 'YES';
                  break;
             }
         }
        String returnUrl = ApexPages.currentPage().getParameters().get('retUrl');
        finalURL = URL.getSalesforceBaseUrl().toExternalForm(); 
        
        finalURL=returnUrl;
        NoUpdate =returnUrl;  
                
    }
    //Reassignment Roundrobin class to be called after proper validation of leads
    public void reassignLeads(){
        
        if(validUser.equalsIgnoreCase('YES')){
            for(sObject  singleLead:setCon.getSelected()){  
                   Lead Ld = (Lead)singleLead;
                   LeadIdSet.add(Ld.Id); 
                                          
            }      
            List<Lead> Leadmember = [Select OwnerId,Status,Owner.ISACTIVE,Id,User_Office_Code__c from Lead where id IN:LeadIdSet];
            Total = Leadmember.size();
            NoUpdateInt = 0;
            for(Lead LD:Leadmember){
                    
                   //User Office Code is not there for Leads will throw exception, so excluding those
                      if(LD.User_Office_Code__c == NULL){
                          NoUpdateInt = NoUpdateInt +1;
                      }
                      else{
                          //Invalid Leads are excluded from being reassigned
                          if(LD.Status != 'Invalid Lead'){
                               userLeadList.add(LD);
                               LeadMapListUser.put(LD.Id,LD);
                               OfficeStr.add(Ld.User_Office_Code__c);
                           }
                       }
            }                                     
                             
            if(userLeadList.size() > 0) reassignUserLeads(userLeadList);                   
            Noassign = Leadmember.size() - userLeadList.size() - NoUpdateInt ;
            assignmentStatus = 'SUCCESS';
        }
    }
    //Calling User and sharkpool owned leads Round Robin
    public void reassignUserLeads(list<lead> leadList){
        OORLeadRoundRobinUser.executeRoundRobinUser(userLeadList,LeadMapListUser,OfficeStr);
       
    }    

}